/**
 * 
 */
package ch.comtools.servermanager.ssh.ui.wizard.page;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import ch.comtools.servermanager.ssh.SSH;

/**
 * @author Roger Dudler <roger.dudler@gmail.com>
 * @version $Id$
 */
public class NewSecureShellConnectionWizardPage extends WizardPage {

	/** widgets */
	private TabItem tabItemGeneral;
	private TabItem tabItemAdvanced;
	private TabItem tabItemProxy;
	private Button btnLabelUseDefaultLabel;
	private Button btnLabelUseCustomLabel;
	private Text txtLabelCustomLabel;
	private Text txtUser;
	private Text txtPassword;
	private Text txtHost;
	
	/** data */
	private String label = "";
	private String user = "";
	private String password = "";
	private String host = "";
	private int port = SSH.DEFAULT_PORT;
	
	//private String proxyHost;
	//private String proxyUser;
	//private String proxyPassword;
	//private int proxyPort;

	/**
	 * @param pageName
	 */
	public NewSecureShellConnectionWizardPage(String pageName) {
		super(pageName);
		this.setTitle(pageName);
		this.setDescription("Define SSH terminal information. You can specify additional settings for proxy connections.");
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		TabFolder folder = new TabFolder(parent, SWT.NONE);
		tabItemGeneral = this.createGeneralTab(folder);
		tabItemAdvanced = this.createAdvancedTab(folder);
		tabItemProxy = this.createProxyTab(folder);
		this.setControl(folder);
		this.setPageComplete(false);
	}
	
	/**
	 * @param folder
	 * @return
	 */
	private TabItem createGeneralTab(TabFolder folder) {
		GridLayout layout = new GridLayout();
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		TabItem item = new TabItem(folder, SWT.NONE);
		item.setText("General");
		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(layout);
		composite.setLayoutData(layoutData);
		
		// create host information group
		this.createGroupHost(composite);
		
		// label group
		layout = new GridLayout();
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		Group groupLabel = new Group(composite, SWT.NONE);
		groupLabel.setLayout(layout);
		groupLabel.setText("Label");
		groupLabel.setLayoutData(layoutData);
		
		// button: use default label
		this.btnLabelUseDefaultLabel = new Button(groupLabel, SWT.RADIO);
		this.btnLabelUseDefaultLabel.setText("Use connection information as label");
		this.btnLabelUseDefaultLabel.setSelection(true);
		this.btnLabelUseDefaultLabel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (btnLabelUseDefaultLabel.getSelection()) {
					txtLabelCustomLabel.setEnabled(false);
				} else {
					txtLabelCustomLabel.setEnabled(true);
				}
			}
		});
		
		// button: use custom label
		this.btnLabelUseCustomLabel = new Button(groupLabel, SWT.RADIO);
		this.btnLabelUseCustomLabel.setText("Use a custom label:");
		
		// text: custom label
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		this.txtLabelCustomLabel = new Text(groupLabel, SWT.BORDER);
		this.txtLabelCustomLabel.setText("");
		this.txtLabelCustomLabel.setEnabled(false);
		this.txtLabelCustomLabel.setLayoutData(layoutData);
		
		// group: authentication
		this.createGroupAuthentication(composite);
		
		// set control for tab item
		item.setControl(composite);
		return item;
	}
	
	/**
	 * Create a group with host information.
	 * @param parent
	 * @return
	 */
	private Group createGroupHost(Composite parent) {
		GridLayout layout = new GridLayout(2, false);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(layout);
		group.setText("Host");
		group.setLayoutData(layoutData);
		
		// label: host
		layoutData = new GridData();
		layoutData.widthHint = 100;
		Label lblHost = new Label(group, SWT.NONE);
		lblHost.setLayoutData(layoutData);
		lblHost.setText("IP / Hostname" + ":");
		
		// text: host
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		txtHost = new Text(group, SWT.BORDER);
		txtHost.setLayoutData(layoutData);
		txtHost.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				host = txtHost.getText();
				validate();
			}
		});
		txtHost.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				txtHost.selectAll();
			}
			public void focusLost(FocusEvent e) {
				validate();
			}
		});
		
		return group;
	}
	
	/**
	 * @param parent
	 * @return
	 */
	private Group createGroupAuthentication(Composite parent) {
		GridLayout layout = new GridLayout(2, false);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(layout);
		group.setText("Authentication");
		group.setLayoutData(layoutData);
		
		// label: user
		layoutData = new GridData();
		layoutData.widthHint = 100;
		Label lblUser = new Label(group, SWT.NONE);
		lblUser.setLayoutData(layoutData);
		lblUser.setText("Username" + ":");
		
		// text: user
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		txtUser = new Text(group, SWT.BORDER);
		txtUser.setLayoutData(layoutData);
		txtUser.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				user = txtUser.getText();
				validate();
			}
		});
		txtUser.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				txtUser.selectAll();
			}
			public void focusLost(FocusEvent e) {
				validate();
			}
		});
		
		// label: password
		layoutData = new GridData();
		Label lblPassword = new Label(group, SWT.NONE);
		lblPassword.setLayoutData(layoutData);
		lblPassword.setText("Password" + ":");
		
		// text: password
		layoutData = new GridData(GridData.FILL_HORIZONTAL);
		txtPassword = new Text(group, SWT.BORDER | SWT.PASSWORD);
		txtPassword.setLayoutData(layoutData);
		txtPassword.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				password = txtPassword.getText();
			}
		});
		txtPassword.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				txtPassword.selectAll();
			}
		});
		
		// button: save password
		
		// label: secret data warning, see subversive
		
		return group;
	}
	
	/**
	 * @param folder
	 * @return
	 */
	private TabItem createAdvancedTab(TabFolder folder) {
		TabItem item = new TabItem(folder, SWT.NONE);
		item.setText("Advanced");
		Composite composite = new Composite(folder, SWT.NONE);
		Button button = new Button(composite, SWT.PUSH);
		button.setText("Bla");
		item.setControl(composite);
		return item;
	}
	
	/**
	 * @param folder
	 * @return
	 */
	private TabItem createProxyTab(TabFolder folder) {
		TabItem item = new TabItem(folder, SWT.NONE);
		item.setText("Proxy");
		Composite composite = new Composite(folder, SWT.NONE);
		Button button = new Button(composite, SWT.PUSH);
		button.setText("Bla");
		item.setControl(composite);
		return item;
	}
	
	/**
	 * Validate form data.
	 */
	private void validate() {
		if (this.btnLabelUseDefaultLabel.getSelection()) {
			if (this.user == null || "".equals(this.user)) {
				this.txtLabelCustomLabel.setText(this.host);
			} else {
				this.txtLabelCustomLabel.setText(this.user + "@" + this.host);
			}
		}
		this.label = this.txtLabelCustomLabel.getText();
		this.setErrorMessage(null);
		this.setPageComplete(true);
		if (this.user == null || "".equals(this.user)) {
			this.setErrorMessage("Please provide a username.");
			this.setPageComplete(false);
		}
		if (this.host == null || "".equals(this.host)) {
			this.setErrorMessage("Please provide a hostname or IP address.");
			this.setPageComplete(false);
		}
	}

	/**
	 * @return
	 */
	public TabItem getTabItemGeneral() {
		return this.tabItemGeneral;
	}
	
	/**
	 * @return
	 */
	public TabItem getTabItemAuthentication() {
		return this.tabItemAdvanced;
	}
	
	/**
	 * @return
	 */
	public TabItem getTabItemProxy() {
		return this.tabItemProxy;
	}
	
	/**
	 * @return
	 */
	public String getHost() {
		return host;
	}
	
	/**
	 * @return
	 */
	public String getUser() {
		return user;
	}
	
	/**
	 * @return
	 */
	public String getPassword() {
		return this.password;
	}
	
	/**
	 * @return
	 */
	public int getPort() {
		return port;
	}
	
	/**
	 * @return
	 */
	public String getLabel() {
		return label;
	}
	
	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		this.txtHost.setFocus();
	}
}
